/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoadFrame.java
 *
 * Created on 5/10/2009, 7:36:45 AM
 */

package nz.ac.massey.spidernetpn.gui;

import java.util.List;

import nz.ac.massey.spidernetpn.database.GraphDatabase;

/**
 *
 * @author Eric
 */
public class LoadFrame extends javax.swing.JFrame {
	MainFrame mfRef;
	GraphDatabase graphDB;
	
    /** Creates new form LoadFrame */
    public LoadFrame(MainFrame mf) {
    	mfRef = mf;
    	graphDB = new GraphDatabase(mf.getFilePath());
        initComponents();
        graphList.setModel(new javax.swing.AbstractListModel() {
            List strings = graphDB.getListOfStoredGraphs();
            public int getSize() { return strings.size(); }
            public Object getElementAt(int i) { return strings.toArray()[i]; }
        });
    }
    
    public javax.swing.JList getJlist(){
    	return graphList;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        graphToLoadLabel = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        graphList = new javax.swing.JList();
        loadButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setResizable(false);

        graphToLoadLabel.setText(" Select Graph To Load");

        jScrollPane1.setViewportView(graphList);

        loadButton.setText("Load");
        loadButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                loadButtonActionPerformed(evt);
            }
        });

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(254, 254, 254)
                        .addComponent(loadButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addComponent(graphToLoadLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(graphToLoadLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelButton)
                    .addComponent(loadButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_loadButtonActionPerformed
        // TODO add your handling code here:
    	mfRef.jProgressBar_MainFrame.setIndeterminate(true);
        int SelectedGraphIndex = graphList.getSelectedIndex();

    	List avaliableGraphs = graphDB.getListOfStoredGraphs();

    	
    	if(SelectedGraphIndex!=-1){
    		mfRef.loadGraph((String)avaliableGraphs.toArray()[SelectedGraphIndex]);
    	}

    	this.setVisible(false);

    }//GEN-LAST:event_loadButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
    	this.setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
    * @param args the command line arguments
    */
//    public static void main(String args[]) {
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                new LoadFrame().setVisible(true);
//            }
//        });
//    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JList graphList;
    private javax.swing.JLabel graphToLoadLabel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton loadButton;
    // End of variables declaration//GEN-END:variables

}
